# 这里是自定义中间件，也可以换成自带的auth中间件，因为本次使用了websocket子协议protocol，所以使用自定义中间件做安全校验
from django.conf.urls import url
from django.urls import path, include

from apps.chat.consumers import ChatConsumer
from apps.chat.middleware import WebSocketAuthMiddleware
# 这里就是自带的auth中间件，没有用到
from channels.auth import AuthMiddlewareStack
# 继承settings中的allow host
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from apps.chat import routing as chat_route
from apps.websocket import routing as ws_route
from apps.websocket.consumers import LogsConsumerSync

routingList = []
routingList.extend(chat_route.websocket_urlpatterns)
routingList.extend(ws_route.ws_urlpatterns)

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            # url(r'ws/logs/(?P<id>\w+)/$', LogsConsumerSync.as_asgi()),
            # url(r'ws/chat/(?P<room_id>\w+)/$', ChatConsumer.as_asgi()),
            routingList
        )
    ),
})

# application = ProtocolTypeRouter({
#     # (http->django views is added by default)
#     # 默认处理http协议
#     # websocket协议处理
#     "websocket": AllowedHostsOriginValidator(
#         WebSocketAuthMiddleware(    # 中间件
#             URLRouter(
#                 # 这里配置websocket的路由
#                 routing.websocket_urlpatterns
#             )
#         )
#     ),
# })
